<template>
  <div class="productDetail">
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item v-for="item in gallery" :key="item.id" >
        <img :src="item.img_url" alt="" width="100%" height="250px" style="display: block"/>
      </van-swipe-item>
<!--      info: {id: 1127025, category_id: 1010002, goods_sn: '1127025', name: '女式蝶边真丝内裤', brand_id: 0, …-->
    </van-swipe>
<!--    产品标签-->
    <Tips />
<!--    产品信息-->
    <div class="info">
      <div class="title">{{info.name}}</div>
      <div class="goods_brief">{{info.goods_brief}}</div>
      <div class="retail_price">{{info.retail_price | RMBformat}}</div>
    </div>

    <van-cell title="展示弹出层" is-link @click="isSkuShow=!isSkuShow"/>

<!--    商品参数-->
    <div class="attribute">
      <h3>商品参数</h3>
      <ul>
        <li v-for="(item,key) in attribute" :key="key">
          <div class="l">{{item.name+":"}}</div>
          <div class="r van-ellipsis">{{item.value}}</div>
        </li>
      </ul>
    </div>

<!--    商品详情-->
    <div class="products_desc" v-html="products_desc" ></div>

<!--    底部常见文字描述-->
    <div class="title0">
      <span>常见问题</span>
    </div>
    <ul class="issue">
      <li v-for="item in issue" :key="item.id">
        <h3>{{item.question}}</h3>
        <p>{{item.answer}}</p>
      </li>
    </ul>

<!--    商商参数-->
    <Products :goodsList="productDetail" ></Products>

    <van-sku
      ref="sku"
      v-model="isSkuShow" :sku="sku" :goods="goods"
      :hide-stock="sku.hide_stock"
    />

<!--    底部商品购买及购物车-->
    <GoodsActive @goToCart="goToCart" :cartcount="cartcount"/>
  </div>
</template>

<script>
import { addProductToCart, getCartCount, GetProductRelated, GetProductsDetailById } from '@/request/api'
import Tips from '@/components/Tips'
import GoodsActive from '@/components/GoodsActive'
import Products from '@/components/Products'

export default {
  name: 'ProductDetail',
  components: { Products, GoodsActive, Tips },
  data () {
    return {
      gallery:[],       //轮播图数据
      info:{},          //商品信息
      attribute:[],     //商品参数
      products_desc:[],   //商品详情信息
      productDetail:[],    //产品相关数据
      issue:[],         //问题描述
      isSkuShow:false,
      sku:{
        tree:[],
        stock:false,
        price:'0',
        stock_num:0,

      },
      goods:{
        picture:'',

      },
      cartcount:0,      //购物车数量
      productList:[],

    }
  },
  created () {
    this.getCartCount()
    this.getProductDetailById()
    this.getProductRelated()
  },
  computed: {},
  methods: {
    // 获取购物车从数量
    getCartCount(){
      getCartCount().then(res=>{

        this.cartcount=res.data.cartTotal.goodsCount
      })
    },
    // 根据id，获取商品信详细数据
    getProductDetailById(){
      GetProductsDetailById({
        id:this.$route.query.id
      }).then(res=>{
        this.gallery=res.data.gallery
        this.info=res.data.info
        this.attribute=res.data.attribute
        this.products_desc=this.info.goods_desc
        this.issue=res.data.issue
        // sku 数据
        this.goods.picture=this.info.list_pic_url
        this.sku.price=this.info.retail_price
        this.sku.stock_num=this.info.goods_number

        this.productList=res.data.productList
      })
    },
    // 根据id，获取产品的相关数据
    getProductRelated(){
      GetProductRelated({id:this.$route.query.id}).then(res=>{

        this.productDetail=res.data.goodsList
      })
    },
    // 点击购物车
    goToCart(){
      // 没有展示sku，则展示，如果展示了，则加入购物车
      if(this.isSkuShow){
        console.log("加入购物车")
        // 获取要添加的商品数量
        console.log()
        addProductToCart({
          goodsId: this.$route.query.id ,
          productId: this.productList[0].id  ,
          number: this.$refs.sku.getSkuData().selectedNum
        }).then(res=>{
          console.log(res)
          // 更新购物车数量
          this.cartcount=res.data.cartTotal.goodsCoun
          // 跳转到购物车页面
          this.$toast.success("加入购物车成功")
          setTimeout(()=>{
            this.$router.push('/cart')
          },1500)

        })
      }else {
        this.isSkuShow=true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.productDetail{
  font-size: .13rem;
}
//商品信息样式
.info{
  background-color: #fff;
  text-align: center;
  line-height: .4rem;
  padding: .10rem 0;
  border-bottom: 1px solid #ccc;

  .title{
    font-size:.2rem;
  }

  .retail_price{
    color: darkred;
  }
}
//商品参数样式
.attribute{
  background-color: #fff;
  margin-top: .2rem;
  padding: .1rem .168rem;
  h3{
    font-size: 0.1638rem;
    line-height: .5rem;
  }
  li{
    height: .40rem;
    line-height: .40rem;
    background-color: #efefef;
    display: flex;
    margin-bottom: .10rem;
    border-radius: .05rem;
    .l{
      width: 30%;

      text-align: right;
      margin-right: .1rem;
      font-size: .12rem;
    }
    .r{
      flex: 1;
      font-size: .13rem;
    }
  }

}
//深度样式写法，穿透ui组件框架样式，直接添加在标签上
/deep/.products_desc{
  img{
    display: block;
    width: 100%;
  }

}

.title0 {
  width: 100%;
  background: #fff;
  height: 0.5rem;
  position: relative;
  &::before {
    content: "";
    width: 50%;
    height: 2px;
    background: #ccc;
    position: absolute;
    top: 50%;
    margin-top: -1px;
    left: 50%;
    margin-left: -25%;
  }
  span {
    width: 30%;
    position: relative;
    background: #fff;
    display: block;
    text-align: center;
    margin: 0 auto;
    height: 0.5rem;
    line-height: 0.5rem;
  }
}
.issue {
  padding: 0 4%;
  background: #fff;
  li {
    h3 {
      height: 0.3rem;
      line-height: 0.3rem;
      padding-left: 0.2rem;
      position: relative;
      font-weight: normal;
      &::before {
        content: "";
        width: 4px;
        height: 4px;
        background: darkred;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 50%;
        margin-top: -2px;
      }
    }
    p {
      font-size: 0.12rem;
      line-height: 0.2rem;
      padding-left: 0.2rem;
    }
  }
}
</style>
